import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_shop_z/config/service_url.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_shop_z/pages/category_page.dart';
import 'package:flutter_shop_z/pages/category_page/bloc/category_bloc.dart';
import 'package:flutter_shop_z/pages/category_page/bloc/category_id_bloc.dart';
import 'package:flutter_shop_z/pages/index_page/bloc/index_count_bloc.dart';

//大分类
class TopNavigator extends StatelessWidget {
  final List navigtorList;
  int _index = 0;
  TopNavigator(this.navigtorList);
  Widget _gridViewItemUI(BuildContext context, item, index) {
    return InkWell(
      onTap: () {
        print("item['categoryName']===>${item['categoryName']}");
        print("item['categoryName']===>${item['categoryId']}");
        BlocProvider.of<CategoryIdBloc>(context).add(SetCategoryIdEvent(index));
        BlocProvider.of<IndexCountBloc>(context).add(SetIndexCountEvent(1));
        //这个跳转暂时有问题
        // Navigator.push(
        //   context,
        //   MaterialPageRoute(builder: (context) {
        //     return CategoryPage(index);
        //   }),
        // );
      },
      child: Column(
        children: <Widget>[
          Image.network(
            "${serviceUrl}${item['categoryImg']}",
            height: ScreenUtil().setHeight(80),
          ),
          Text(item['categoryName'])
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    //超过10个做处理
    // if (navigtorList.length > 10) {
    //   navigtorList.removeRange(10, navigtorList.length);
    // }
    return Container(
      height: ScreenUtil().setHeight(280),
      padding: EdgeInsets.all(3),
      child: GridView.count(
        //easyRefresh的1.0版本与GridView的滑动有冲突，可以禁用GridView的滑动来解决
        //easyRefresh的2.0版本没有这个冲突
        // physics: NeverScrollableScrollPhysics(),
        crossAxisCount: 5,
        padding: EdgeInsets.all(5),
        children: navigtorList.map((item) {
          return _gridViewItemUI(context, item, _index++);
        }).toList(),
      ),
    );
  }
}
